<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<script type="text/javascript">
	var roleId;
	function initTabPage(id) {
		roleId=id;
		$("#menuData tr").not(".dataTableHead").remove();
		var path='<%=request.getContextPath()%>/menu.do?method=getMenusByRoleId&id='+id;
		$.getJSON(path,function(data,status,xhr){
			data=data[0];
			var lastActionNum=0
			for(var i=0;i<data.length;i++){
				var menuTrHtml="<tr>";
				menuTrHtml+="<td rowno='"+(i+1+lastActionNum)+"'>"+(i+1+lastActionNum)+"</td>";
				menuTrHtml+="<td><img id='menuImg_"+data[i]['id']+"' class='menuTrImg' status='open' src='<%=request.getContextPath()%>/images/butExpand.gif'/>&nbsp;";
				if(data[i]['viewMode']=='Y'){
					menuTrHtml+="<input	class='menuTrCheckbox' type='checkbox' value='"+data[i]['id']+"' checked/>";
				}else{
					menuTrHtml+="<input	class='menuTrCheckbox' type='checkbox' value='"+data[i]['id']+"' />";
				}
				menuTrHtml+="<img align='absmiddle'	src='<%=request.getContextPath()%>/images/icons/icon003a1.gif'>&nbsp;"+data[i]['menuName']+"</td>";
				menuTrHtml+="</tr>";
				for(var j=0;j<data[i]['actions'].length;j++){
					var actionData=data[i]['actions'][j];
					var actionTrHtml="<tr class='actionTr' parentImg='menuImg_"+data[i]['id']+"'>";
					actionTrHtml+="<td rowno='"+(i+j+2+lastActionNum)+"'>"+(i+j+2+lastActionNum)+"</td>";
					actionTrHtml+="<td><q style='padding: 0 10px'></q>";
					actionTrHtml+="<img	src='<%=request.getContextPath()%>/images/butNoChild.gif'>&nbsp;";
					if(actionData['viewMode']=='Y'){
					actionTrHtml+="<input type='checkbox' value='"+actionData['id']+"' class='actionTrCheckbox' checked>";
					}else{
					actionTrHtml+="<input type='checkbox' value='"+actionData['id']+"' class='actionTrCheckbox'>";
					}
					actionTrHtml+="<img	align='absmiddle' src='<%=request.getContextPath()%>/"+actionData['icon']+"'>&nbsp;";
					actionTrHtml+=actionData['actionName']+"</td>";
					actionTrHtml+="</tr>";
					menuTrHtml+=actionTrHtml;
				}
				lastActionNum+=data[i]['actions'].length;
				$(menuTrHtml).appendTo($("#menuData").find('tbody'));
				console.log($(".dataTableHead").siblings());
				$(".dataTableHead").siblings().unbind('mouseover').bind('mouseover',function(){
					$(this).addClass('jqgrowHover');
				});
				$(".dataTableHead").siblings().unbind('mouseout').bind('mouseout',function(){
					$(this).removeClass('jqgrowHover');
				});
				$(".dataTableHead").siblings().unbind('click').bind('click',function(){
					if(!$(this).addClass('jqgrid-tr-selected').attr('selected')){
						$(this).addClass('jqgrid-tr-selected').attr('selected',true);
					}else{
						$(this).removeClass('jqgrid-tr-selected').removeAttr('selected');
					}
					$(this).siblings().not(".dataTableHead").removeClass('jqgrid-tr-selected');
				});
			}
		});
	}
	$().ready(function(){
		$(".menuTrImg").die('click').live('click',function(){
			var oldSrc=$(this).attr('src');
			var newSrc="";
			var id=$(this).attr('id');
			if($(this).attr('status')=='open'){
				$(this).attr('status','close');
				newSrc=oldSrc.replace("butExpand","butCollapse");
				$('.actionTr').each(function(index,obj){
					var parentId=$(this).attr('parentImg');
					if(parentId==id){
						$(this).hide();
					}
				});
			}else{
				$(this).attr('status','open');
				newSrc=oldSrc.replace("butCollapse","butExpand");
				$('.actionTr').each(function(index,obj){
					var parentId=$(this).attr('parentImg');
					if(parentId==id){
						$(this).show();
					}
				});
			}
			$(this).attr('src',newSrc);
		});
		$(".menuTrCheckbox").die('click').live('click',function(){
			var trImageId=$(this).prev().attr('id');
			if($(this).attr('checked')){//该状态为点击后的checkbox选中状态
				$(".actionTr").each(function(index,obj){
					if($(this).attr('parentimg')==trImageId){
						$(this).find(".actionTrCheckbox").attr('checked','true');
					}
				});
			}else{
				$(".actionTr").each(function(index,obj){
					if($(this).attr('parentimg')==trImageId){
						$(this).find(".actionTrCheckbox").removeAttr('checked');
					}
				});
			}
		});
		
	});
	function clickAllSelect(obj){
		if($(obj).attr('checked')){
			$("#menuData :checkbox").attr('checked','true');
		}else{
			$("#menuData :checkbox").removeAttr('checked');
		}
	}
	function save(){
		var actionIds = "";
		$('.actionTrCheckbox').each(function(index,obj){
			if($(this).attr('checked')){
				actionIds+=$(this).val()+",";
			}
		});
		console.log(actionIds);
		var path='<%=request.getContextPath()%>/role.do?method=addActionsToRole&id='+roleId+'&actionids='+actionIds;
		$.getJSON(path,function(data,status,xhr){
			createAlertDialog('提示信息',data['msg']);
		});
	}
</script>
<table width="100%" cellspacing="0" cellpadding="2" >
<tr>
    <td style="padding:4px 5px;" id="uid_td3">
      <span style="line-height:24px;">&nbsp;&nbsp;&nbsp;<label>全选&nbsp;<input type="checkbox" onclick="clickAllSelect(this);" class="inputCheckbox"></label></span>
    <a href="javascript:void(0);"  onclick="save();"class="zPushBtn" tabindex="-1" id="" ><img src="<%=request.getContextPath() %>/images/icons/icon018a4.gif"><b>保存&nbsp;</b></a>
  	</td>
  </tr>
</table>
<table width="100%" cellspacing="0" cellpadding="2" id="menuData" class="dataTable">
	<tbody>
		<tr class="dataTableHead">
			<td width="6%" id="uid_td5">序号</td>
			<td width="94%" id="uid_td6">菜单名称</td>
		</tr>
		<tr id="uid_tr99" style="background-color: rgb(216, 247, 157);">
			<td rowno="1" id="uid_td7">1</td>
			<td id="uid_td8"><img
				src="<%=request.getContextPath()%>/images/butExpand.gif">&nbsp;<input
				type="checkbox" value="120" id="dg1_TreeRowCheck_1"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon003a1.gif">&nbsp;文档工作台</td>
		</tr>
		<tr>
			<td rowno="2" id="uid_td9">2</td>
			<td id="uid_td10"><q style="padding: 0 10px"></q><img
				src="<%=request.getContextPath()%>/images/butNoChild.gif">&nbsp;<input
				type="checkbox" value="129" id="dg1_TreeRowCheck_2"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon003a11.gif">&nbsp;文档编辑</td>
		</tr>
		<tr>
			<td rowno="3" id="uid_td11">3</td>
			<td id="uid_td12"><q style="padding: 0 10px"></q><img
				src="<%=request.getContextPath()%>/images/butNoChild.gif">&nbsp;<input
				type="checkbox" value="303" id="dg1_TreeRowCheck_3"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon018a4.gif">&nbsp;文档审核</td>
		</tr>
		<tr>
			<td rowno="4" id="uid_td13">4</td>
			<td id="uid_td14"><q style="padding: 0 10px"></q><img
				src="<%=request.getContextPath()%>/images/butNoChild.gif">&nbsp;<input
				type="checkbox" value="397" id="dg1_TreeRowCheck_4"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon050a18.gif">&nbsp;文档回收站</td>
		</tr>
		<tr>
			<td rowno="5" id="uid_td15">5</td>
			<td id="uid_td16"><q style="padding: 0 10px"></q><img
				src="<%=request.getContextPath()%>/images/butNoChild.gif">&nbsp;<input
				type="checkbox" value="131" id="dg1_TreeRowCheck_5"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon016a1.gif">&nbsp;个人备忘</td>
		</tr>
		<tr>
			<td rowno="6" id="uid_td17">6</td>
			<td id="uid_td18"><q style="padding: 0 10px"></q><img
				src="<%=request.getContextPath()%>/images/butNoChild.gif">&nbsp;<input
				type="checkbox" value="132" id="dg1_TreeRowCheck_6"
				name="dg1_TreeRowCheck" class="inputCheckbox"><img
				align="absmiddle"
				src="<%=request.getContextPath()%>/images/icons/icon028a7.gif">&nbsp;短消息</td>
		</tr>
	</tbody>
</table>